package com.oe.web;

import com.oe.monitor.TPMonitor;
import com.oe.service.LdmService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

@Controller
public class LdmController {


    ScheduledThreadPoolExecutor scheduledThreadPoolExecutor=new ScheduledThreadPoolExecutor(Runtime.getRuntime().availableProcessors());

    @Autowired
    LdmService ldmService;

    @RequestMapping("/test")
    @ResponseBody
   public String test(){

        return ldmService.returnClientIp();
   }


    /**
     * 每50毫秒3次调用，一分钟共3600次调用
     * @return
     */
    @RequestMapping("/start")
    @ResponseBody
    public String start(){


        scheduledThreadPoolExecutor.scheduleAtFixedRate(new Runnable() {
            @Override
            public void run() {
                ldmService.methodB();
            }
        },0,50, TimeUnit.MICROSECONDS);


        scheduledThreadPoolExecutor.scheduleAtFixedRate(new Runnable() {
            @Override
            public void run() {
                ldmService.methodC();
            }
        },0,50, TimeUnit.MICROSECONDS);



        scheduledThreadPoolExecutor.scheduleAtFixedRate(new Runnable() {
            @Override
            public void run() {
                ldmService.methodD();

            }
        },0,50, TimeUnit.MICROSECONDS);

        return "start:success";

    }

    /**
     * 停止定时调用
     * @return
     */
    @RequestMapping("/stop")
    @ResponseBody
    public String stop(){
        scheduledThreadPoolExecutor.shutdown();
        return "stop:success";
    }
}
